﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
	<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
	<h1>JWT验证方式</h1><br />
	<h2>后台管理员信息</h2>
	<input id="username" type="text" />
	<input id="password" type="text" />
	<input type="button" value="请求TOKEN" onclick="GoToSubmit()" />
	<input type="button" value="刷新TOKEN" onclick="GoToRefreshToken()" />
	<input type="button" value="得到当前登录管理员信息" onclick="GoToAPI()" />
	<br />
	accessToken:<p id="accessToken"></p>
	accessTokenExpired:<p id="accessTokenExpired"></p>
	refreshToken:<p id="refreshToken"></p>
	refreshTokenExpired:<p id="refreshTokenExpired"></p>
	message:<p id="message"></p>
	<br />
	<h2>前台会员信息</h2>
	<input id="username1" type="text" />
	<input id="password1" type="text" />
	<input type="button" value="请求TOKEN" onclick="GoToSubmit1()" />
	<input type="button" value="刷新TOKEN" onclick="GoToRefreshToken1()" />
	<input type="button" value="得到当前登录会员信息" onclick="GoToAPI1()" />
	<br />
	accessToken:<p id="accessToken1"></p>
	accessTokenExpired:<p id="accessTokenExpired1"></p>
	refreshToken:<p id="refreshToken1"></p>
	refreshTokenExpired:<p id="refreshTokenExpired1"></p>
	message:<p id="message1"></p>
	<br />
	<h1>IdentityServer4验证方式</h1><br />
	<h2>后台管理员信息</h2>
	<input id="username2" type="text" />
	<input id="password2" type="text" />
	<input type="button" value="请求TOKEN" onclick="GoToSubmit2()" />
	<input type="button" value="刷新TOKEN" onclick="GoToRefreshToken2()" />
	<input type="button" value="得到当前登录会员信息" onclick="GoToAPI2()" />
	<br />
	accessToken:<p id="accessToken2"></p>
	accessTokenExpired:<p id="accessTokenExpired2"></p>
	refreshToken:<p id="refreshToken2"></p>
	token_type:<p id="token_type2"></p>
	message:<p id="message2"></p>
	<script type="text/javascript">
		function GoToSubmit() {
			var username = $("#username").val();
			var password = $("#password").val();
			jQuery.ajax({
				url: 'http://localhost:57350/api/Token/GetToken?username=' + encodeURIComponent(username) + '&password=' + encodeURIComponent(password),
				type: 'POST',
				timeout: 30000,
				async: false,
				contentType: "application/json",
				error: function () {
					alert("服务器异常！");
					return false;
				},
				success: function (data) {
					if (data.status) {
						$("#accessToken").html(data.accessToken);
						$("#refreshToken").html(data.refreshToken);
						$("#accessTokenExpired").html(data.accessTokenExpired);
						$("#refreshTokenExpired").html(data.refreshTokenExpired);
						$("#message").html(data.msg);
					}
					else {
						$("#message").html(data.msg);
					}
				}
			});
		}
		function GoToRefreshToken() {
			var refreshToken = $("#refreshToken").html();
			jQuery.ajax({
				url: 'http://localhost:57350/api/Token/RefreshToken?token=' + encodeURIComponent(refreshToken) + '&userID=1',
				type: 'POST',
				timeout: 30000,
				async: false,
				contentType: "application/json",
				error: function () {
					alert("服务器异常！");
					return false;
				},
				success: function (data) {
					if (data.status) {
						$("#accessToken").html(data.accessToken);
						$("#refreshToken").html(data.refreshToken);
						$("#accessTokenExpired").html(data.accessTokenExpired);
						$("#refreshTokenExpired").html(data.refreshTokenExpired);
						$("#message").html(data.msg);
					}
					else {
						$("#message").html(data.msg);
					}
				}
			});
		}
		function GoToAPI() {
			var access_token = $("#accessToken").html();
			if (access_token == null || access_token == "") {
				alert("没有TOKEN");
				return;
			}
			jQuery.ajax({
				url: 'http://localhost:57350/api/Token/GetAdminInfo',
				type: 'POST',
				timeout: 10000,
				async: false,
				contentType: "application/json",
				beforeSend: function (xhr) {
					xhr.setRequestHeader('Authorization', 'Bearer ' + access_token)
				},
				error: function () {
					alert("服务器异常！");
					return false;
				},
				success: function (data) {
					$("#message").html(data.data);
				}
			});
		}

		function GoToSubmit1() {
			var username = $("#username1").val();
			var password = $("#password1").val();
			jQuery.ajax({
				url: 'http://localhost:57350/api/UserToken/GetToken?username=' + encodeURIComponent(username) + '&password=' + encodeURIComponent(password),
				type: 'POST',
				timeout: 30000,
				async: false,
				contentType: "application/json",
				error: function () {
					alert("服务器异常！");
					return false;
				},
				success: function (data) {
					if (data.Status) {
						$("#accessToken1").html(data.AccessToken);
						$("#refreshToken1").html(data.RefreshToken);
						$("#accessTokenExpired1").html(data.AccessTokenExpired);
						$("#refreshTokenExpired1").html(data.RefreshTokenExpired);
						$("#message1").html(data.Msg);
					}
					else {
						$("#message1").html(data.Msg);
					}
				}
			});
		}
		function GoToRefreshToken1() {
			var refreshToken = $("#refreshToken1").html();
			jQuery.ajax({
				url: 'http://localhost:57350/api/UserToken/RefreshToken?token=' + encodeURIComponent(refreshToken) + '&userID=1',
				type: 'POST',
				timeout: 30000,
				async: false,
				contentType: "application/json",
				error: function () {
					alert("服务器异常！");
					return false;
				},
				success: function (data) {
					if (data.Status) {
						$("#accessToken1").html(data.AccessToken);
						$("#refreshToken1").html(data.RefreshToken);
						$("#accessTokenExpired1").html(data.AccessTokenExpired);
						$("#refreshTokenExpired1").html(data.RefreshTokenExpired);
						$("#message1").html(data.Msg);
					}
					else {
						$("#message1").html(data.Msg);
					}
				}
			});
		}
		function GoToAPI1() {
			var access_token = $("#accessToken1").html();
			if (access_token == null || access_token == "") {
				alert("没有TOKEN");
				return;
			}
			jQuery.ajax({
				url: 'http://localhost:57350/api/UserToken/GetUserInfo',
				type: 'POST',
				timeout: 10000,
				async: false,
				contentType: "application/json",
				beforeSend: function (xhr) {
					xhr.setRequestHeader('Authorization', 'Bearer ' + access_token)
				},
				error: function () {
					alert("服务器异常！");
					return false;
				},
				success: function (data) {
					$("#message1").html(data.data);
				}
			});
		}

		function GoToSubmit2() {
			var username = $("#username2").val();
			var password = $("#password2").val();
			var parms = "client_id=JXWebApi&client_secret=JXWebApi&grant_type=password&username=" + encodeURIComponent(username) + '&password=' + encodeURIComponent(password);
			jQuery.ajax({
				url: 'http://127.0.0.1:9000/connect/token?' + parms,
				type: 'POST',
				timeout: 30000,
				async: false,
				contentType: "application/json",
				error: function () {
					alert("服务器异常！");
					return false;
				},
				success: function (data) {
					if (data.error == "") {
						$("#accessToken2").html(data.access_token);
						$("#refreshToken2").html(data.refresh_token);
						$("#accessTokenExpired2").html(data.expires_in);
						$("#token_type2").html(data.token_type);
						$("#message2").html(data.scope);
					}
					else {
						$("#message2").html(data.error);
					}
				}
			});
		}
		function GoToRefreshToken2() {
			var refreshToken = $("#refreshToken2").html();
			var parms = "client_id=JXWebApi&client_secret=JXWebApi&grant_type=refresh_token&refresh_token=" + encodeURIComponent(refreshToken);
			jQuery.ajax({
				url: 'http://localhost:9000/connect/token?' + parms,
				type: 'POST',
				timeout: 30000,
				async: false,
				contentType: "application/json",
				error: function () {
					alert("服务器异常！");
					return false;
				},
				success: function (data) {
					if (data.error == "") {
						$("#accessToken2").html(data.access_token);
						$("#refreshToken2").html(data.refresh_token);
						$("#accessTokenExpired2").html(data.expires_in);
						$("#token_type2").html(data.token_type);
						$("#message2").html(data.scope);
					}
					else {
						$("#message2").html(data.error);
					}
				}
			});
		}
		function GoToAPI2() {
			var access_token = $("#accessToken2").html();
			if (access_token == null || access_token == "") {
				alert("没有TOKEN");
				return;
			}
			jQuery.ajax({
				url: 'http://localhost:57350/api/test/GetInfo',
				type: 'POST',
				timeout: 10000,
				async: false,
				contentType: "application/json",
				beforeSend: function (xhr) {
					xhr.setRequestHeader('Authorization', 'Bearer ' + access_token)
				},
				error: function () {
					alert("服务器异常！");
					return false;
				},
				success: function (data) {
					$("#message2").html(data.data);
				}
			});
		}
	</script>
</body>
</html>